﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Windows.Forms;
using System.Windows.Threading;
using IWshRuntimeLibrary;
using Microsoft.Win32;


namespace Mouse_Trapper
{
    public partial class MouseTrapper : Form
    {
        
        public MouseTrapper()
        {
           SystemEvents.SessionSwitch += GetMouseToPrimaryScreen;
            InitializeComponent();
            
          
          
        }

        

      private bool Mousedown = false;
      private int xdifference, ydifference;
        private void MouseButDown(object sender, MouseEventArgs e)
        {
            Mousedown = true;
             xdifference = Cursor.Position.X - ActiveForm.Location.X;
             ydifference = Cursor.Position.Y - ActiveForm.Location.Y;
        }

        private void MouseButIp(object sender, MouseEventArgs e)
        {
            Mousedown = false;
        }

        private void MouseMoved(object sender, MouseEventArgs e)
        {
            if (Mousedown == true)
            {
                
               
               ActiveForm.SetDesktopLocation(Cursor.Position.X - xdifference, Cursor.Position.Y-ydifference);

            }
           
        }

        int ScreenNumber;
        

        private void SetupClip()
        {
            Screen[] Screens = new Screen[Screen.AllScreens.Count()];
            Screens = Screen.AllScreens;
            CurrentScreen CurrentScreenClass = new CurrentScreen();
            int ScreenPointerIsIn;
            ScreenPointerIsIn = CurrentScreenClass.CurrentScreenOfPointer(Cursor.Position,Screens);
            timer1.Stop();
          ClippingRectangle = new Rectangle(Screens[ScreenPointerIsIn].Bounds.Location, Screens[ScreenPointerIsIn].Bounds.Size);
          ScreenNumber = ScreenPointerIsIn;
            timer1.Start();
        }

        Rectangle ClippingRectangle;
        private Settings1 settings = new Settings1();
      

        public void GetMouseToPrimaryScreen(object sender, EventArgs e)
    {
        timer1.Stop();
        Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width/2, Screen.PrimaryScreen.Bounds.Height/2);
        SetupClip();
    }

        private void Form1_Load(object sender, EventArgs e)
        {
           
           SetupClip();
            
            if(settings.FirstTime==true)
            {

                ContinueButton.Visible = true;
                InfoBox.Visible = true;
                
                settings.FirstTime = false;
                settings.Save();
            }
            if (settings.TrackNumOfCrossings == true)
            {
                InstallationCount.Text = settings.TotalCrossings.ToString();
            }


            StartWithWindows.Checked = settings.StartWithWindows;
            StartInTray.Checked = settings.StartInTray;
            TrackNumOfCrossings.Checked = settings.TrackNumOfCrossings;
            PassportKey.SelectedIndex = settings.PassportKeyChoice;
            PassportKey_SelectedIndexChanged(this, e);

           

        }


        private void FirstShown(object sender, EventArgs e)
        {
            if (settings.StartInTray == true)
            {
                SendToTrayButton_Click(this, e);
            }

            else{

            try
            {

                SendKeys.Send("%");
                WindowState = FormWindowState.Normal;
            }
            catch
            {

                Close();
            }
            }

        }

        private void ContinueButton_Click(object sender, EventArgs e)
        {
            ContinueButton.Visible = false;
            InfoBox.Visible = false;
        }

        private void CloseButton_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void SendToTrayButton_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
            ShowInTaskbar = false;

            TrayIcon.Visible = true;
        }

    
        private void TrayIcon_MouseClick(object sender, MouseEventArgs e)
        {
            WindowState = FormWindowState.Normal;
            ShowInTaskbar = true;
           
            TrayIcon.Visible = false;
        }
        private string PassportKeyVar;
        private void PassportKey_SelectedIndexChanged(object sender, EventArgs e)
        {
           
            settings.PassportKeyChoice = PassportKey.SelectedIndex;
            settings.Save();
            PassportKeyVar = PassportKey.SelectedItem.ToString();
       
        }

        private void StartWithWindows_CheckedChanged(object sender, EventArgs e)
        {
            settings.StartWithWindows = StartWithWindows.Checked;
            settings.Save();
            IWshRuntimeLibrary.IWshShortcut Shortcut;
            IWshRuntimeLibrary.WshShell IShell = new WshShell();
            string ShortcutLocation = Environment.GetFolderPath(Environment.SpecialFolder.Startup)+@"\Mouse Trapper.lnk";
            if (StartWithWindows.Checked)
            {
                
                try
                {
                    Shortcut = IShell.CreateShortcut(ShortcutLocation);
                    Shortcut.TargetPath = Application.ExecutablePath;
                    Shortcut.Save();
                    
                }
                catch (Exception ex) { MessageBox.Show("Sorry! There was an error:" + ex.ToString() + "\nThis was probably caused by inadequate permissions. \nIf problem persists email JABFreeware.com@gmail.com"); }
            }
            if (!StartWithWindows.Checked)
            {
                try
                {
                    FileInfo ShortcutDir = new System.IO.FileInfo(ShortcutLocation);
                    ShortcutDir.Delete();
                }
                catch (Exception ex) { MessageBox.Show("Sorry! There was an error:" + ex.ToString() + "\nThis was probably caused by inadequate permissions. \nIf problem persists email JABFreeware.com@gmail.com"); }
            }
            
           
        }

        private void TrackMouseMileage_CheckedChanged(object sender, EventArgs e)
        {
            settings.StartInTray = StartInTray.Checked;
            settings.Save();
        }

        private void TrackNumOfCrossings_CheckedChanged(object sender, EventArgs e)
        {
            settings.TrackNumOfCrossings = TrackNumOfCrossings.Checked;
            settings.Save();
        }

        private void InfoBox_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //this order is important , obviously!
            if (Screen.AllScreens.Count() > ScreenNumber && Screen.AllScreens[ScreenNumber].Bounds.Size != ClippingRectangle.Size)
            {
                SetupClip();
            }

            Cursor.Clip = ClippingRectangle;

            
        }
        bool keycurrentlypressed;
        int NumberOfCrossing;
        
        System.Windows.Input.KeyConverter KeyConversion = new System.Windows.Input.KeyConverter();
        Rectangle OLDREG;
        private void timer2_Tick(object sender, EventArgs e)
        {
            if (System.Windows.Input.Keyboard.IsKeyDown((System.Windows.Input.Key)KeyConversion.ConvertFromString(PassportKeyVar)) == true)
            {
                
                OLDREG = ClippingRectangle;
                keycurrentlypressed = true;
                timer1.Stop();
                Cursor.Clip = new Rectangle(0, 0, 0, 0);
            }
            
         

            
           if (System.Windows.Input.Keyboard.IsKeyUp((System.Windows.Input.Key)KeyConversion.ConvertFromString(PassportKeyVar)) == true && keycurrentlypressed == true)
           {
               keycurrentlypressed = false;
               SetupClip();
               if (TrackNumOfCrossings.Checked==true && ClippingRectangle !=OLDREG)
               {

                   NumberOfCrossing++;
                   StartupCountText.Text = NumberOfCrossing.ToString();

                   InstallationCount.Text = Convert.ToString(NumberOfCrossing + settings.TotalCrossings);
                  
               }
               
           }
      

        }

        private void MouseTrapper_FormClosing(object sender, FormClosingEventArgs e)
        {
            settings.TotalCrossings = NumberOfCrossing;
            settings.Save();
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void TrayIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {

        }

      
    }
}
